﻿using System;
using System.Collections.Generic;
using System.Security.Policy;
using Close.io.Client;
using Newtonsoft.Json;

namespace Close.io.API
{
	public class Contact :CloseIoLeadObject, IIdentifiable
	{
		#region Properties
		[JsonProperty(PropertyName = "name")]
		public string Name { get; set; }

		[JsonProperty(PropertyName = "title")]
		public string Title { get; set; }

		[JsonProperty(PropertyName = "phones")]
		public IEnumerable<Phone> Phones { get; set; }

		[JsonProperty(PropertyName = "urls")]
		public IEnumerable<ContactUrl> Urls { get; set; }

		[JsonProperty(PropertyName = "emails")]
		public IEnumerable<Email> Emails { get; set; } 
		#endregion

		public void AddPhone(Phone phone)
		{
			if (phone == null) throw new ArgumentNullException("phone");

			if (Phones == null)
				Phones = new List<Phone>();
					
			((List<Phone>)Phones).Add(phone);
		}

		public void AddPhone(string phoneNumber, ContactMethodType type)
		{
			if (phoneNumber == null) throw new ArgumentNullException("phoneNumber");
			AddPhone(new Phone(phoneNumber, type));
		}

		public void AddEmail(Email emailAddress)
		{
			if (emailAddress == null) throw new ArgumentNullException("emailAddress");

			if (Emails == null)
				Emails = new List<Email>();

			((List<Email>)Emails).Add(emailAddress);
		}

		public void AddUrl(string url, ContactMethodType type)
		{
			if (url == null) throw new ArgumentNullException("url");
			AddUrl(new ContactUrl(url, type));
		}


		public void AddUrl(ContactUrl contactUrl)
		{
			if (contactUrl == null) throw new ArgumentNullException("contactUrl");

			if (Urls == null)
				Urls = new List<ContactUrl>();

			((List<ContactUrl>)Urls).Add(contactUrl);
		}

		public void AddEmail(string emailAddress, ContactMethodType type)
		{
			if (emailAddress == null) throw new ArgumentNullException("emailAddress");
			AddEmail(new Email(emailAddress, type));
		}


		public Contact(string name, string title = null)
		{
			if (name == null) throw new ArgumentNullException("name");
			Name = name;
			Title = title;
			 
		}

		public Contact()
		{
			// TODO: Complete member initialization
		}

		public class ContactUrl
		{
			[JsonProperty(PropertyName = "url")]
			public string Url { get; set; }

			[JsonProperty(PropertyName = "type")]
			public string Type { get; set; }

			public ContactUrl(string url, ContactMethodType type)
			{
				if (url == null) throw new ArgumentNullException("url");
				Url = url;
				Type = Enum.GetName(typeof(ContactMethodType), type);
			}

			public ContactUrl()
			{
			}

		}
		public class Email
		{
			[JsonProperty(PropertyName = "email")]
			public string EmailAddress { get; set; }

			[JsonProperty(PropertyName = "type")]
			public string Type { get; set; }

			public Email(string emailAddress, ContactMethodType type)
			{
				if (emailAddress != null) EmailAddress = emailAddress;
				Type = Enum.GetName(typeof(ContactMethodType), type);
			}

			public Email()
			{
			}
		}

		public class Phone
		{
			[JsonProperty(PropertyName = "phone")]
			public string PhoneNumber { get; set; }

			[JsonProperty(PropertyName = "phone_formatted")]
			public string PhoneFormatted { get; private set; }

			[JsonProperty(PropertyName = "type")]
			public string Type { get; set; }

			public Phone(string phoneNumber, ContactMethodType type)
			{
				if (phoneNumber == null) throw new ArgumentNullException("phoneNumber");
				PhoneNumber = phoneNumber;
				Type = Enum.GetName(typeof(ContactMethodType), type);
			}

			public Phone()
			{
			}
		}

		public enum ContactMethodType
		{
			Office, Home, Mobile, Direct, Fax, Url, Other
		}
	}
}